package com.myhouse.common.utils.myquartz.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

/**
 * 依赖注入配置类
 */
@Component
public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory{
    /**
     * AutowireCapableBeanFactory
     */
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    /**
     * 将job实例交给spring ioc托管
     * 我们在job实例实现类内可以直接使用spring注入的调用被spring ioc管理的实例
     * @param bundle
     * @return
     * @throws Exception
     */
    @Override
    protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
        final Object jobInstance = super.createJobInstance(bundle);
        /**
         * 将job实例交付给spring ioc
         */
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}